package com.bzkj.web.controller.ordermanagement.outbound;

import com.bzkj.common.annotation.LogB;
import com.bzkj.common.core.controller.BaseController;
import com.bzkj.common.core.domain.AjaxResult;
import com.bzkj.common.core.page.TableDataInfo;
import com.bzkj.common.core.text.Convert;
import com.bzkj.common.domain.NccRequestDTO;
import com.bzkj.common.enums.BusinessType;
import com.bzkj.common.utils.ServletUtils;
import com.bzkj.document.domain.DocConfirmDTO;
import com.bzkj.document.domain.saleout.dto.SaleOutQueryDTO;
import com.bzkj.document.service.salesoutbound.SalesOutboundService;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.util.HashMap;
import java.util.Map;

import static com.bzkj.common.core.page.TableSupport.PAGE_NUM;
import static com.bzkj.common.core.page.TableSupport.PAGE_SIZE;

/**
 * @projectName: bzkj-wms
 * @package: com.bzkj.web.controller.ordermanagement.outbound
 * @className: SalesOutboundController
 * @description: 销售出库单据处理控制层
 * @author: C1309
 * @createDate: 2024年12月05日
 * @version: v1.0
 */
@RestController
@RequestMapping("/sales-outbound")
public class SalesOutboundController extends BaseController {

    /**
     * 获取NCC销售出库单据服务层
     */
    @Resource
    private SalesOutboundService salesOutboundService;


    /**
     * 获取NCC销售出库单据
     *
     * @return 查询结果
     */
    @GetMapping("/ncc")
    @PreAuthorize("@ss.hasPermi('order:saleout:list')")
    public TableDataInfo selectNccSalesOutbound(@RequestParam Map<String, Object> requestBody) {
        NccRequestDTO nccRequestDTO = getNccRequestDTO(requestBody);
        if (nccRequestDTO.getFbillflag() == null || nccRequestDTO.getFbillflag().isEmpty()){
            nccRequestDTO.setFbillflag("2");
        }
        return salesOutboundService.selectOutboundOrder(nccRequestDTO);
    }

    /**
     * 确认销售出库单据
     *
     * @param docConfirmDTO 确认信息
     * @return 确认结果
     */
    @PostMapping("/confirm")
    @PreAuthorize("@ss.hasPermi('order:saleout:confirm')")
    @LogB(title = "销售出库单据", businessType = BusinessType.CONFIRM)
    public AjaxResult insertSalesOutBoundOrder(@RequestBody DocConfirmDTO docConfirmDTO) {
        docConfirmDTO.setLoginUser(getLoginUser());
        return salesOutboundService.insertSalesOutBoundOrder(docConfirmDTO);
    }

    /**
     * 获取本地销售出库单据
     *
     * @param saleOutQueryDTO 查询条件
     * @return 查询结果
     */
    @GetMapping
    @PreAuthorize("@ss.hasPermi('order:saleout:list')")
    public TableDataInfo getLocalSalesOutbound(SaleOutQueryDTO saleOutQueryDTO) {
        if (saleOutQueryDTO == null) {
            saleOutQueryDTO = new SaleOutQueryDTO();
        }
        Integer pageNum = Convert.toInt(ServletUtils.getParameter(PAGE_NUM), 1);
        Integer pageSize = Convert.toInt(ServletUtils.getParameter(PAGE_SIZE), 10);
        return salesOutboundService.selectLocalSaleOutHeader(saleOutQueryDTO, pageNum, pageSize);
    }

    /**
     * 销售出库单签字
     *
     * @param cgeneralhid NCC单据表头主键
     * @return 签字结果
     */
    @PutMapping("/{cgeneralhid}")
    @PreAuthorize("@ss.hasPermi('order:saleout:audit')")
    @LogB(title = "销售出库单据", businessType = BusinessType.SIGN)
    public AjaxResult signatureSalesOutBound(@PathVariable String cgeneralhid) {
//                if (nccAjaxResult.isSuccess()) {
//            // 调用物流服务（向TMS推送物流信息）
//            AjaxResult tmsAjaxResult = tmsController.sendLogistic(header);
//            if (!tmsAjaxResult.isSuccess()) {
//                return tmsAjaxResult;
//            }
//        }
        return salesOutboundService.signatureSalesOutBound(cgeneralhid, getLoginUser());
    }

    /**
     * 更新销售出库单据
     *
     * @param responseBody 需更新的数据
     * @return 更新结果
     */
    @PostMapping("/update")
    @PreAuthorize("@ss.hasPermi('order:saleout:edit')")
    @LogB(title = "销售出库单据", businessType = BusinessType.UPDATE, pkFieldNames = "cgeneralhid",
            tableNames = "wms_warehouse_out_product",
            detailTable = "wms_warehouse_out_detail_product"
    )
    public AjaxResult updateSalesOutBoundOrder(@RequestBody HashMap<String, Object> responseBody) {
        return salesOutboundService.updateSalesOutBoundOrder(responseBody, getLoginUser(), false);
    }

    /**
     * 销售出库单回退
     *
     * @param headerId 需要打回的单据类型
     * @return 回退结果
     */
    @PostMapping("/back/{headerid}")
    @PreAuthorize("@ss.hasPermi('order:saleout:edit')")
    @LogB(title = "销售出库单据", businessType = BusinessType.CANCELED)
    public AjaxResult addPurchase(@PathVariable("headerid") String headerId) {
        return salesOutboundService.orderBack(headerId, getLoginUser());
    }

    /**
     * 失败库存处理
     * @param headerId 表头主键
     * @return 处理结果
     */
    @PostMapping("/inventory/{headerid}")
    @LogB(title = "销售出库单据", businessType = BusinessType.INVENTORY)
    @PreAuthorize("@ss.hasPermi('order:saleout:audit')")
    public AjaxResult updateInventory(@PathVariable("headerid") String headerId){
        return salesOutboundService.updateInventory(headerId, getLoginUser());
    }

    /**
     * 删除销售出库单据
     *
     * @param headerId 需要删除的单据类型
     * @return 删除结果
     */
    @DeleteMapping("/{headerid}")
    @PreAuthorize("@ss.hasPermi('order:saleout:delete')")
    @LogB(title = "销售出库单据", businessType = BusinessType.INVALID)
    public AjaxResult deleteSalesOutBoundOrder(@PathVariable("headerid") String headerId,
                                               @RequestParam(name = "reason", defaultValue = "") String reason) {
        if (reason == null || reason.trim().isEmpty()) {
            return AjaxResult.error("作废原因似乎没有填写，请填写作废原因后重试！");
        }
        return salesOutboundService.deleteOtherOutBoundOrder(headerId, getLoginUser(), reason);
    }
}
